package com.zhkj.lib.base

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import com.zhkj.lib.utils.RxBus
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable

open class BaseViewModel(application: Application) : AndroidViewModel(application) {

    private val loginStatusLiveData = MutableLiveData<LoginTokenInvalidEvent>()
    private val accountChangeLiveData = MutableLiveData<AccountChangeEvent>()
    private val balanceChangeLiveData = MutableLiveData<BalanceChangeEvent>()
    private val userInfoChangeLiveData = MutableLiveData<UserInfoChangeEvent>()
    private val cartChangeLiveData = MutableLiveData<CartChangeEvent>()
    private val memberStatusChangeLiveData = MutableLiveData<MemberStatusChangeEvent>()
    private val logoutLiveData = MutableLiveData<LogoutEvent>()

    protected val dispose: CompositeDisposable = CompositeDisposable()

    public override fun onCleared() {
        if (dispose.size() > 0 && !dispose.isDisposed) {
            dispose.dispose()
        }
    }

    /**
     * 登录状态失效事件观察者
     */
    fun loginStatusSubscribe(): MutableLiveData<LoginTokenInvalidEvent> {
        val subscribe =
            RxBus.toObservable(LoginTokenInvalidEvent::class.java).observeOn(AndroidSchedulers.mainThread())
                .subscribe({
                    loginStatusLiveData.value = it
                }, { it.printStackTrace() })
        dispose.add(subscribe)
        return loginStatusLiveData
    }

    /**
     * 切换账户消息观察者
     */
    fun accountChangeSubscribe(): MutableLiveData<AccountChangeEvent> {
        val subscribe = RxBus.toObservable(AccountChangeEvent::class.java)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({ accountChangeLiveData.value = it }, { it.printStackTrace() })
        dispose.add(subscribe)
        return accountChangeLiveData
    }

    /**
     * 余额变动消息观察者
     */
    fun balanceChangeSubscribe(): MutableLiveData<BalanceChangeEvent> {
        val subscribe = RxBus.toObservable(BalanceChangeEvent::class.java)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({ balanceChangeLiveData.value = it }, { it.printStackTrace() })
        dispose.add(subscribe)
        return balanceChangeLiveData
    }

    /**
     * 用户信息变动消息观察者
     */
    fun userInfoChangeSubscribe(): MutableLiveData<UserInfoChangeEvent> {
        val subscribe = RxBus.toObservable(UserInfoChangeEvent::class.java)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({ userInfoChangeLiveData.value = it }, { it.printStackTrace() })
        dispose.add(subscribe)
        return userInfoChangeLiveData
    }

    /**
     * 购物车变动消息观察者
     */
    fun cartChangeSubscribe(): MutableLiveData<CartChangeEvent> {
        val subscribe = RxBus.toObservable(CartChangeEvent::class.java)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({ cartChangeLiveData.value = it }, { it.printStackTrace() })
        dispose.add(subscribe)
        return cartChangeLiveData
    }

    /**
     * 会员状态变动观察者
     */
    fun memberStatusChangeSubscribe(): MutableLiveData<MemberStatusChangeEvent> {
        val subscribe = RxBus.toObservable(MemberStatusChangeEvent::class.java)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({ memberStatusChangeLiveData.value = it }, { it.printStackTrace() })
        dispose.add(subscribe)
        return memberStatusChangeLiveData
    }

    /**
     * 退出登录观察者
     */
    fun logoutSubscribe(): MutableLiveData<LogoutEvent> {
        val subscribe = RxBus.toObservable(LogoutEvent::class.java)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({ logoutLiveData.value = it }, { it.printStackTrace() })
        dispose.add(subscribe)
        return logoutLiveData

    }
}

/**
 * 登录状态失效的消息通知
 */
class LoginTokenInvalidEvent(val tokenInvalid: Boolean = true)

/**
 * 切换账户消息通知
 */
class AccountChangeEvent

/**
 * 余额变动消息通知
 */
class BalanceChangeEvent

/**
 * 用户资料变动消息通知
 */
class UserInfoChangeEvent

/**
 * 购物车变动消息通知
 */
class CartChangeEvent

/**
 * 会员资格变动消息通知
 */
class MemberStatusChangeEvent

/**
 * 退出登录消息通知
 */
class LogoutEvent